package com.thssh.ui.lsn10.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/25
 * email: zhangyugehu@163.com
 */

public class LoadingView extends View {

    private Paint mPaint;
    private Path mPath;
    private Path mDstPath;
    private PathMeasure mPathMeasure;
    private float mPathLength;

    private ValueAnimator mLoadingAnimator;
    private float mStartDx;
    private float mEndDx;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPath = new Path();
        mDstPath = new Path();
        initPath();
        mPathMeasure = new PathMeasure(mPath, false);
        mPathLength = mPathMeasure.getLength();
        initAnimator();
    }

    private void initAnimator() {
        mLoadingAnimator = ValueAnimator.ofFloat(0, 1);
        mLoadingAnimator.setDuration(2000);
        mLoadingAnimator.setRepeatMode(ValueAnimator.RESTART);
        mLoadingAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mLoadingAnimator.setInterpolator(new FastOutSlowInInterpolator());
        mLoadingAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mEndDx = mPathLength * value;
                if(value < 0.5f) {
                    mStartDx = 0;
                }else{
                    mStartDx = mPathLength * (value - 0.5f) * 2;
                }
                invalidate();
            }
        });
        mLoadingAnimator.start();
    }

    private void initPath() {
        mPath.reset();
        mPath.addCircle(150, 150, 80, Path.Direction.CW);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(mLoadingAnimator != null){
            mLoadingAnimator.cancel();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mDstPath.reset();
        mPathMeasure.getSegment(mStartDx, mEndDx, mDstPath, true);
        canvas.drawPath(mDstPath, mPaint);
    }
}
